package com.chiao.action.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chiao.core.BaseController;
import com.chiao.core.IntEditor;
import com.chiao.entity.admin.Role;
import com.chiao.service.admin.RoleService;

@Controller
@RequestMapping(value = "/admin/role")
public class RoleAction extends BaseController{

	@Autowired RoleService roleService;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(int.class, new IntEditor());
	}
	
	@RequestMapping(value="/list")
	public @ResponseBody List<Role> list(){
		return roleService.listRole();
	}
        
	@RequestMapping(value = "/add")
	public @ResponseBody String addDept(Role r) {
		long roleid=roleService.addRole(r);
		return "{success:true,roleid:"+roleid+"}";
	}
	@RequestMapping(value = "/update",method=RequestMethod.POST)
	public @ResponseBody String updateDept(Role r) {
		if (roleService.updateRole(r) > 0) {
			return "{success:true}";
		} else {
			return "{failure:true}";
		}
	}
	
	@RequestMapping(value="/delete")
	public @ResponseBody int deleteRole(@RequestParam int id){
		return roleService.deleteRole(id);
	}
	
}
